﻿//  Symplectic Spark
//  Copyright 2010 Symplectic Ltd
//  Created by Martyn Whitwell (martyn@symplectic.co.uk)

//  This file is part of Spark.

//  Spark is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.

//  Spark is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.

//  You should have received a copy of the GNU General Public License
//  along with Spark.  If not, see <http://www.gnu.org/licenses/>.

// $URL: https://symplectic-spark.googlecode.com/svn/trunk/website/App_Code/InternalFunctions.cs $
// $LastChangedDate: 2010-05-05 14:09:38 +0100 (Wed, 05 May 2010) $
// $LastChangedRevision: 40 $
// $LastChangedBy: martyn@symplectic.co.uk $

using System;
using System.Web;


/// <summary>
/// Summary description for Component
/// </summary>
namespace Symplectic.Spark
{
    public abstract class InternalFunctions :BaseType
    {
        #region Logger
        private static readonly log4net.ILog Logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        #endregion

        public static string GetValue(string functionName, RequestSpec requestSpec)
        {
            switch (functionName)
            {
                case "query":
                    string query = requestSpec["query"];
                    if (query != null && query.Trim().Length > 0)
                        return query.Trim();
                    else
                        return "search";

                case "query-categories":
                    string categories = "";
                    foreach (string key in requestSpec.Request.QueryString.AllKeys)
                    {
                        if (key.StartsWith("search-checkbox-") && HttpUtility.HtmlEncode(requestSpec.Request.QueryString[key]) == "true")
                            categories += HttpUtility.HtmlEncode(key.Replace("search-checkbox-", "")) + ",";
                    }
                    if (categories == "")
                        categories = "activities,concepts,equipment,grants,org-structures,projects,publications,users";
                    else
                        categories = categories.TrimEnd(new char[] { ',' });
                    return categories;


                case "search-checkbox-publications":
                    return BuildCheckbox(functionName, "Publications", requestSpec);
                case "search-checkbox-grants":
                    return BuildCheckbox(functionName, "Grants", requestSpec);
                case "search-checkbox-activities":
                    return BuildCheckbox(functionName, "Professional Activities", requestSpec);
                case "search-checkbox-org-structures":
                    return BuildCheckbox(functionName, "Organisational Structures", requestSpec);
                case "search-checkbox-projects":
                    return BuildCheckbox(functionName, "Projects", requestSpec);
                case "search-checkbox-equipment":
                    return BuildCheckbox(functionName, "Equipment", requestSpec);
                case "search-checkbox-users":
                    return BuildCheckbox(functionName, "People", requestSpec);
                case "search-checkbox-concepts":
                    return BuildCheckbox(functionName, "Concepts", requestSpec);

                case "template-filename":
                    return requestSpec.CurrentElementTypeTemplate.Template;
                case "element-title":
                    return requestSpec.CurrentElementTypeTemplate.Title;
                case "element-type":
                    if (requestSpec.CurrentElementType != null)
                        return requestSpec[requestSpec.CurrentElementType.Name];
                    else
                        return null;

                case "date":
                    return DateTime.Now.ToString();
                case "application-path":
                    return Global.ApplicationPathVirtual;
                case "home-page":
                    return Global.ApplicationPathVirtual + "/index.html";
                case "public-path":
                    return Global.PublicPathVirtual;
                case "version":
                    return Global.Version + "." + "$LastChangedRevision: 40 $".Split(new char[] {':'})[1].TrimEnd(new char[]{'$'}).Trim();

                case "page":
                    string page = requestSpec["page"];
                    if (page != null && page.Trim().Length > 0)
                        return page.Trim();
                    else
                        return "1";
                case "per-page":
                    string perPage = requestSpec["per-page"];
                    if (perPage != null && perPage.Trim().Length > 0)
                        return perPage.Trim();
                    else
                        return Global.PerPage;
                
                case "debug-mode":
                    return Global.DebugMode.ToString();
                case "debug-render-size":
                case "debug-render-time":
                    return "##" + functionName + "##";
                                    
                default:
                    return "[function: " + functionName + " is undefined or cannot be evaluated]";
            }
        }

        private static string BuildCheckbox(string name, string label, RequestSpec requestSpec)
        {
            string output = "<input type=\"checkbox\" name=\"" + name + "\" id=\"" + name + "\" value=\"true\"";
            if (requestSpec != null && requestSpec.Request != null && requestSpec[name] != null && bool.Parse(requestSpec[name]))
                output += " checked=\"checked\"";
            output += "/><label for=\"" + name + "\">" + label  + "</label>";
            return output;
        }
    }
}

